<template>
  <div class="app-container">
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['official:desc:add']"
        >新增</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table
      v-loading="loading"
      :data="list"
    >
      <el-table-column prop="orderNum" label="排序" width="60"></el-table-column>
      <el-table-column prop="title" label="标题" width="150"></el-table-column>
      <el-table-column prop="content" label="内容" width="200" ></el-table-column>
      <el-table-column prop="articleId" label="关联文章" :show-overflow-tooltip="true">
        <template slot-scope="scope">
          <span v-if="articleList.find(f=>f.articleId==scope.row.articleId)">{{articleList.find(f=>f.articleId==scope.row.articleId).title}}</span>
        </template>
      </el-table-column>
      <el-table-column label="图片" >
        <template slot-scope="scope">
          <el-image style="height: 100px" :src="downloadApi+scope.row.descImg" :preview-src-list="[downloadApi+scope.row.descImg]" />
        </template>
      </el-table-column>
      <el-table-column prop="remark" label="备注" width="150"></el-table-column>
      <el-table-column prop="status" label="状态" width="80">
        <template slot-scope="scope">
          <el-switch
            v-model="scope.row.status"
            @change="(val) => updateStatus(scope.row, val)"
            active-value="0"
            inactive-value="1">
          </el-switch>
        </template>
      </el-table-column>
      
      <el-table-column label="创建时间" align="center" prop="createTime">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button 
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['official:desc:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['official:desc:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <!-- 添加或修改菜单对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="60vw" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="auto">
        <el-row>
          <el-col :span="24">
            <el-form-item label="标题" prop="title">
              <el-input v-model="form.title" controls-position="right" />
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="内容" prop="content">
              <el-input type="textarea" :rows="5" v-model="form.content" controls-position="right" />
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item prop="articleId">
              <span slot="label">
                <el-tooltip content="点击菜单跳转到对应的文章" placement="top">
                <i class="el-icon-question"></i>
                </el-tooltip>
                绑定文章
              </span>
                <el-select v-model="form.articleId" filterable placeholder="请选择" style="width: 100%;">
                  <el-option
                    v-for="item in articleList"
                    :key="item.articleId"
                    :label="item.title"
                    :value="item.articleId+''">
                  </el-option>
                </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="显示排序" prop="orderNum">
              <el-input-number v-model="form.orderNum" controls-position="right" :min="0" />
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="备注" prop="remark">
              <el-input v-model="form.remark" controls-position="right" />
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="图片">
              <el-upload
                class="upload-demo"
                action="#"
                :limit="1"
                :show-file-list="true"
                ref="upload_projectImg"
                :auto-upload="false"
                :http-request="function(file){return submitProjectImg(file)}"
                list-type="picture">
                <el-button size="small" type="primary">点击上传</el-button>
              </el-upload>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listDesc, getDesc, delDesc, addDesc, updateDesc } from "@/api/official/desc";
import downloadApi, { uploadFuncAwait } from '@/utils/upload'
import { listArticle } from "@/api/official/article";

export default {
  name: "Desc",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 显示搜索条件
      showSearch: true,
      // 菜单表格树数据
      list: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        title: [
          { required: true, message: "标题不能为空", trigger: "blur" }
        ],
        content: [
          { required: true, message: "描述内容不能为空", trigger: "blur" }
        ],
        orderNum: [
          { required: true, message: "显示顺序不能为空", trigger: "blur" }
        ]
      },
      projectImgFile:'',
      downloadApi: downloadApi,
      articleList:[],//文章list
    };
  },
  created() {
    this.getList();
    this.getArticleList();
  },
  methods: {
    getArticleList(){
      listArticle(this.queryParams).then(response => {
        this.articleList = response.rows;
      });
    },
    /**
     * 获取上传路径
     * @param file
     */
    submitProjectImg(file) {
      this.projectImgFile = file.file;
    },
    /** 查询菜单列表 */
    getList() {
      this.loading = true;
      listDesc().then(response => {
        this.list = response.data;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        orderNum: undefined,
        title: "",
        content: "",
        remark: "",
        status: "0",
        articleId:'/'
      };
      this.projectImgFile = ""
      this.resetForm("form");
    },
    /** 新增按钮操作 */
    handleAdd(row) {
      this.reset();
      this.open = true;
      this.title = "添加轮播图";
      this.$refs.upload_projectImg.clearFiles()
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      this.form = {...row};
      this.open = true;
      this.title = "修改轮播图";
      this.$refs.upload_projectImg.clearFiles()
    },
    /** 变更状态操作 */
    updateStatus(row,val) {
        this.form = {...row};
        this.form.status = val
        updateDesc(this.form).then(response => {
            this.$modal.msgSuccess("修改成功");
            this.getList();
        });
    },
    /** 提交按钮 */
    submitForm: function() {
      this.$refs["form"].validate(async valid => {
        if (valid) {

          let postUrlImg = '/upload/uploadImage'

          this.$refs.upload_projectImg.submit()
          if(this.projectImgFile)
          {
            let formData = new FormData()
            formData.append('multipartFile', this.projectImgFile)
            let ret = await uploadFuncAwait(postUrlImg, formData)
            this.$set(this.form, "descImg", ret.data.msg)
          }

          if (this.form.descId != undefined) {
            updateDesc(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addDesc(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      this.$modal.confirm('是否确认删除？').then(function() {
        return delDesc(row.descId);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    }
  }
};
</script>
